cap program drop myeclplot
program myeclplot
	* Description: eclplot with distributed lag commands using parmested .dta files
	* Outputs: 1. Contemporaneous, vectors allowed 2. Distlag / cumul, graph by vector element 
	* Variables: 
	* byvar: Naming option if plotting event study (distributed lag or cumulative effect) by x variable
	* con: Naming option for plotting contemporaneous response for each x variable 
	* cum: Naming option for plotting cumulative response for each x variable 
	* concum: Naming option for plotting both contemporaneous and cumulative response for each x variable
	* concum2: Naming option for plotting both contemporaneous and cumulative response (and second last cumulative response) for each x variable
	* all: Naming option for plotting all cumulative responses in same graph for each x variable 
	* xdim: Dimensionality of x variable (scalar or vector); xlab: Label x variable; xname: Name of x variable 
	* leads: number of leads; lags: number lags; addleads: Option to add leads to event study 
	* diff: Level of differencing (default 1)
	* extra_commands: Extra commands
	
	* Default schemes? e.g. scheme(plotplainblind) with minimal color and scheme(plotplain) for black and white 
	
	syntax [if] [in] [, byvar(str) con(str) cum(str) concum(str) concum2(str) all(str) xdim(str) xlab(str) xname(str) leads(str) lags(str) addleads diff(str) extra_commands(str)]
	
	* Drop controls 
	cap gen parmno=_n 
	* qui sum parmno 
	* local ub = `r(max)' - `controlsdim'
	if "`diff'"=="" local diff=1 
	if "`addleads'"=="" local ub = `xdim'*(1+`lags'/`diff')
	if "`addleads'"!="" local ub = `xdim'*(1+`lags'/`diff'+`leads'/`diff') 
	keep if parmno<=`ub'
	
	* Generate event time 
	cap gen event_time=.
	label var event_time "Event Time" 
	if "`addleads'"!="" local start=-`leads'
	if "`addleads'"=="" local start=0 
	local i=-1 
	foreach n of numlist `start'(`diff')`lags' {
		local i=`i'+1
	 	cap replace event_time = `n' if parmno>`i'*`xdim'&parmno<=(`i'+1)*`xdim'
	}
	
	* Number and label each vector element 
	cap bys event_time: gen varno=_n
	label var varno "`xname'" 
	local eclplot_commands `"eplot(con) ciopts(recast(rconnected) lp(dash)) graphregion(color(white)) bgcolor(white) yline(0,lp(dash))"'
	cap noi label define xlab `xlab'
	cap noi label val varno xlab 

	* 1. Plot cumulative response by bin 
	if "`byvar'"!="" {
	eclplot estimate min95 max95 event_time, `eclplot_commands' by(varno, graphregion(color(white)) plotregion(color(white)) bgcolor(white) note("")) subtitle("", fcolor(white) lcolor(white)) `extra_commands'
	translate @Graph "`byvar'.pdf", name("Graph")
	}
	
	* 2. Plot contemporaneous response 
	if "`con'"!="" {
	eclplot estimate min95 max95 varno if event_time==0, `eclplot_commands' ytitle(`ytitle') xlab(`xlab', labsize(vsmall)) `extra_commands'
	translate @Graph "`con'.pdf", name("Graph")
	}
	
	* 3. Plot cumulative response 
	if "`cum'"!="" {
	eclplot estimate min95 max95 varno if event_time==`lags', `eclplot_commands' ytitle(`ytitle') xlab(`xlab', labsize(vsmall)) `extra_commands'
	translate @Graph "`cum'.pdf", name("Graph")
	}
	
	* 4. Plot contemporaneous and cumulative response 
	if "`concum'"!="" {
	eclplot estimate min95 max95 varno if event_time==0|event_time==`lags', `eclplot_commands' supby(event_time) ytitle(`ytitle') xlab(`xlab', labsize(vsmall)) `extra_commands'
	translate @Graph "`concum'.pdf", name("Graph")
	}
	
	* 5. Plot con, cum, and second last to show plateau 
	if "`concum2'"!="" {
	eclplot estimate min95 max95 varno if event_time==0|event_time==`lags'|event_time==`lags'-`diff', `eclplot_commands' supby(event_time) ytitle(`ytitle') xlab(`xlab', labsize(vsmall)) `extra_commands'
	translate @Graph "`concum2'.pdf", name("Graph")
	}
	
	* 6. Plot cumulative responses all in same graph (very messy, not suggested) 
	if "`all'"!="" {
	eclplot estimate min95 max95 varno, supby(event_time) xtitle(Event time) ytitle(`ytitle') `eclplot_commands' xlab(`xlab', labsize(vsmall)) `extra_commands'
	translate @Graph "`all'.pdf", name("Graph")
	}
	
end	
